package com.jie.other.jetpack

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jie.commonlib.base.BaseFragment
import com.jie.other.R
import com.jie.other.bean.ui.ViewModelItemBean
import com.jie.other.databinding.OtherFragmentViewModelListBinding
import com.jie.other.jetpack.viewmodel.UserViewModel

class ViewModelListFragment : BaseFragment(){

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val mViewBinding = OtherFragmentViewModelListBinding.inflate(inflater, container, false)

        val mViewModel by activityViewModels<UserViewModel>()
        mViewModel.getUserList().observe(viewLifecycleOwner) {
            mViewBinding.rvList.adapter?.notifyDataSetChanged()
        }

        mViewBinding.rvList.adapter = object : BaseQuickAdapter<ViewModelItemBean, BaseViewHolder>
            (R.layout.other_adapter_view_model_list, mViewModel.getUserList().value){
            override fun convert(holder: BaseViewHolder, item: ViewModelItemBean) {
                holder.getView<TextView>(R.id.tv_content).apply {
                    text = item.name
                    setTextColor(if (item.isSelect) {
                        Color.RED
                    } else {
                        Color.BLACK
                    })
                }

            }

        }.apply {
            setOnItemClickListener { _, _, position ->
                mViewModel.selectUser(position)
            }
        }
        return mViewBinding.root
    }

}